/*
 * PushManager.java
 * business
 *
 * Created by ChenTao on 2016/10/13.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.push;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.JsonParseException;
import com.yidiandao.business.base.BaseActivity;
import com.yidiandao.business.data.bean.Message;
import com.yidiandao.business.data.net.exception.NetErrorHelper;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.receiver.JPushReceiver;
import com.yidiandao.business.receiver.NotificationClickReceiver;
import com.yidiandao.business.utils.GsonProvider;

import cn.jpush.android.api.JPushInterface;

/**
 * 推送管理Manager
 */
public class PushProcessManager {

    private IPushProcess mPushProcess;

    private Context mContext;

    private Message mMessage;

    private PushProcessFactory mFactory;

    private boolean mIsForeground;

    public PushProcessManager(Context context, Bundle bundle, boolean isForeground) {
        if (context == null) {
            throw new NullPointerException("Context can not be null !!!");
        }

        mContext = context;
        mIsForeground = isForeground;

        if (bundle != null) {
            parsePushData(bundle);
        }
    }

    /**
     * 解析推送中的数据
     */
    private void parsePushData(Bundle bundle) {
        // 推送标题
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        // 推送内容
        String content = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        // 推送消息id
        String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);
        // 推送中存储的额外数据
        String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);

        try {
            mMessage = GsonProvider.getGson().fromJson(extra, Message.class);
        } catch (JsonParseException e) {
            NetErrorInfo netErrorInfo = NetErrorHelper.generateNetErrorInfo(e);
            // 打印错误到控制台,并上传部分错误到友盟
            NetErrorHelper.reportError(JPushReceiver.class, "Cause By JPush JSONParseError,check interface", null, netErrorInfo, extra);
        } finally {
            if (mMessage == null) {
                mMessage = new Message();
            }
            mMessage.setTitle(title);
            mMessage.setContent(content);
            mMessage.setMsgId(msgId);
        }
    }

    /**
     * 设置推送消息的类型
     */
    public void setPushProcess(String type) {
        if (mFactory == null) {
            mFactory = new PushProcessFactory();
        }
        mPushProcess = mFactory.getPushProcess(mContext, mMessage, type, mIsForeground);
    }

    /**
     * 处理极光推送消息. <b>该方法仅在{@link JPushReceiver}中调用,以处理推送消息</b>
     */
    public void processPushMessage() {
        // 设置指定的推送类型
        setPushProcess(mMessage.getType());
        if (mPushProcess != null) {
            mPushProcess.processPush();
        }
    }

    /**
     * 获取打开通知跳转页面的Intent. <b>该方法仅在 {@link NotificationClickReceiver} 中调用,该方法中完成了通知栏点击逻辑</b>
     */
    public Intent getOpenNotificationJumpActivityIntent() {
        if (mPushProcess == null) {
            return null;
        }
        return mPushProcess.getClickNotificationJumpActivityIntent();
    }

    /**
     * 点击收到推送弹出的对话框的确认按钮后的处理.<b>该方法仅在{@link BaseActivity}中调用</b>
     */
    public void clickDialogPositiveButton(Message message) {
        if (mPushProcess != null) {
            mPushProcess.onDialogEnsureButtonClick(message);
        }
    }
}
